// 写入
export const setCookie = (
  name,
  value,
  { maxAge, domain, path, secure } = {}
) => {
  let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
  if (typeof maxAge === "nmber") {
    cookieText += `; max-age=${maxAge}`;
  }
  if (domain) {
    cookieText += `; domain=${domain}`;
  }
  if (path) {
    cookieText += `; path=${path}`;
  }
  if (secure) {
    cookieText += `; secure`;
  }
  document.cookie = cookieText;
};

// 读取

export const getCookie = (name) => {
  name = `${encodeURIComponent(name)}`;
  const cookies = document.cookie.split("; ");
  for (let item of cookies) {
    const [cookieName, cookieValue] = item.split("=");
    if (cookieName === name) {
      return decodeURIComponent(cookieValue);
    }
  }
  return undefined;
};

// 删除
export const removeCookie = (name, { domain, path } = {}) => {
  setCookie(name, "", { domain, path, maxAge: -1 });
};
